package leetcode;


/**
 * 设计循环队列
 */
public class LeetCode_622 {
    private int[] values;
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    public LeetCode_622(int k) {
        values = new int[k];
    }

    /**
     * Insert an element into the circular queue. Return true if the operation is successful.
     */
    public boolean enQueue(int value) {
        if (isFull()) return false;
        if (isEmpty()) {
            values[0] = value;
            head = 0;
            tail = 0;
            size = 1;
        } else {
            values[(tail + 1) % values.length] = value;
            tail = (tail + 1) % values.length;
            size++;
        }
        return true;
    }

    /**
     * Delete an element from the circular queue. Return true if the operation is successful.
     */
    public boolean deQueue() {
        if (isEmpty()) return false;
        values[head] = 0;
        head = (head + 1) % values.length;
        size--;
        return true;
    }

    /**
     * Get the front item from the queue.
     */
    public int Front() {
        if (isEmpty()) return -1;
        return values[head];
    }

    /**
     * Get the last item from the queue.
     */
    public int Rear() {
        if (isEmpty()) return -1;
        return values[tail];
    }

    /**
     * Checks whether the circular queue is empty or not.
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * Checks whether the circular queue is full or not.
     */
    public boolean isFull() {
        return size == values.length;
    }
}